package com.kucai.yifaqapp.model;

import android.util.Log;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.ui.activity.BaseActivitySimple;
import com.kucai.yifaqapp.ui.fragment.BaseFragmentSimple;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.io.InterruptedIOException;
import java.net.SocketException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2017/5/12.
 * CustomObserver
 */
public abstract class CustomObserver<T> implements Observer<T> {
    /**
     * @param baseActivity     Activity引用
     * @param dialogCancelable DialogCancelable默认false
     */
    protected CustomObserver(BaseActivitySimple baseActivity, boolean dialogCancelable) {
        this.baseActivity = baseActivity;
        this.dialogCancelable = dialogCancelable;
    }

    /**
     * @param baseFragmentSimple Activity引用
     */
    protected CustomObserver(BaseFragmentSimple baseFragmentSimple) {
        if (baseFragmentSimple.getActivity() instanceof BaseActivitySimple) {
            this.baseActivity = (BaseActivitySimple) baseFragmentSimple.getActivity();
        }
        this.dialogCancelable = dialogCancelable;
    }

    /**
     * @param baseActivity Activity引用
     */
    protected CustomObserver(BaseActivitySimple baseActivity) {
        this.baseActivity = baseActivity;
    }

    /**
     * 不显示Dialog构造方法
     */
    protected CustomObserver() {

    }

    private BaseActivitySimple baseActivity;
    private boolean dialogCancelable;

    @Override
    public void onSubscribe(Disposable d) {
        onSubscribeCustom(d);
        if (baseActivity != null) {
            baseActivity.getDisposableList().add(d);//添加disposableList
            if (baseActivity.dialog != null) {
                baseActivity.dialog.setCancelable(dialogCancelable);
                baseActivity.dialog.show();//自动显示Dialog
            }
        }
    }

    @Override
    public void onNext(T t) {
        if (null != t) {
            try {
                onCustomNext(t);
            } catch (Exception e) {
            }
        }
    }

    /**
     * 经过初筛的返回体
     *
     * @param t
     */
    protected abstract void onCustomNext(T t);

    @Override
    public void onError(Throwable e) {
        SYSOUtils.errLog(e.getMessage(), e);
        dismissDialog();
        onErrorCustom(e);
        if (baseActivity != null) {
            if (e instanceof InterruptedIOException
                    || e instanceof SocketException
                    || e instanceof UnknownHostException) {
                Toast.makeText(baseActivity, R.string.toast_interruptedIOException, Toast.LENGTH_LONG).show();
            } else {
//                Toast.makeText(baseActivity, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }


    @Override
    public void onComplete() {
        dismissDialog();

    }

    /**
     * 隐藏Dialog
     */
    private void dismissDialog() {
        if (baseActivity != null
                && baseActivity.dialog != null
                && baseActivity.dialog.isShowing()) {
            baseActivity.dialog.dismiss();
        }
    }

    /**
     * onSubscribe具体处理
     *
     * @param disposable
     */
    protected void onSubscribeCustom(Disposable disposable) {
    }

    /**
     * 异常具体处理
     *
     * @param e
     */
    protected void onErrorCustom(Throwable e) {
    }
}
